#include "date.h"

const char *jour[]={"lundi","mardi","mercredi","jeudi","vendredi","samedi","dimanche"};
const char *mois[]={"janvier","fevrier","mars","avril","mai","juin","juillet","aout","septembre","octobre","novembre","decembre"};



/* *********************************************************************
         	 Fonction de la structure Date                               
********************************************************************* */
Date * creer_Dat()
{
  Date * date = (Date *) malloc (1 * sizeof(Date));
  if(date == NULL)
    {
      fprintf(stderr, "Mémoire insuffisante");
      exit(EXIT_FAILURE);
    }
  init_Dat(date);
  return date;
}

void init_Dat(Date *date)
{
  if(date)
    {
      date->jour = 0;
      date->mois = 0;
      date->annee = 0;
      date->maj = 0;
      date->sup = 0;
      date->id = 0;
    }
}

void liberer_Dat(Date *date)
{
  if(date)
    {
      date->jour = 0;
      date->mois = 0;
      date->annee = 0;
      date->id = 0;
      date->sup = 0;
      date->maj = 0;
    }
}

void detruire_Dat(Date **date)
{
  if(date)
    {
      liberer_Dat((*date));
      free((*date));
      (*date) = NULL;
    }
}

void copier_Dat(Date ** dateDest, const Date * dateSrc)
{
  if((*dateDest) == NULL)
    {
      (*dateDest) = creer_Dat();
    }
  if(dateSrc)
    {
      setJour_Dat((*dateDest), getJour_Dat(dateSrc));
      setMois_Dat((*dateDest), getMois_Dat(dateSrc));
      setAnnee_Dat((*dateDest), getAnnee_Dat(dateSrc));
      setId_Dat((*dateDest), getId_Dat(dateSrc));
      setMaj_Dat((*dateDest), getMaj_Dat(dateSrc));
      setSup_Dat((*dateDest), getSup_Dat(dateSrc));
    }
  else
    {
      detruire_Dat(dateDest);
      (*dateDest) = NULL;
    }
}

/* *********************************************************************                                                                        
           Accesseurs et Mutateurs de la structure Date                
********************************************************************* */

unsigned int getId_Dat(const Date *date) 
{
  if(date)
    return date->id;
  return 0;
}


void setId_Dat(Date *date, const unsigned int id) 
{
  if(date)
    {
      date->id = id;
    }   
}


unsigned char getJour_Dat(const Date *date)
{
  if(date)
    return date->jour;
  return 0;
}

void setJour_Dat(Date *date, const unsigned char jour)
{
  if(date)
    {
      if(jour == 0) 
	return;
      if(date->mois == 1 || date->mois == 3
	 || date->mois == 5 || date->mois == 7
	 || date->mois == 8 || date->mois == 10
	 || date->mois == 12)
	{
	  if(jour > 31)
	    return;
	} else {
	if(jour > 30)
	  return;
      }
      if(date->mois == 2 && jour > 29)
	return;
      date->jour = jour;
    }
}

unsigned char getMois_Dat(const Date *date) 
{
  if(date)
    return date->mois;
  return 0;
}


void setMois_Dat(Date *date, const unsigned char mois) 
{
  if(date)
    {
      if(mois > 0 && mois < 13)
	date->mois = mois;
    }
}


signed int getAnnee_Dat(const Date *date) 
{
  if(date)
    return date->annee;
  return 0;
}


void setAnnee_Dat(Date *date, const signed int annee) 
{
  if(date)
    {
      date->annee = annee;
    }
   
}

char * getDate_Dat(Date *date)
{
  char *chaine;
  chaine = (char *) malloc ( 12 * sizeof(char));
  sprintf(chaine, "%d/%d/%d", getJour_Dat(date),
	  getMois_Dat(date), getAnnee_Dat(date));
  return chaine;
}

void setMaj_Dat(Date *ptr, const char b)
{
  if(ptr)
    {
      ptr->maj = b;
      if(b == 1)
	ptr->sup = !b;
    }
}

char getMaj_Dat(const Date *ptr)
{
  if(ptr)
    return ptr->maj;
  return 0;
}

void setSup_Dat(Date *ptr, const char b)
{
  if(ptr)
    {
      ptr->sup = b;
      if(b == 1)
	ptr->maj = !b;
    }
}

char getSup_Dat(const Date *ptr)
{
  if(ptr)
    return ptr->sup;
  return 0;
}

void regression_Dat()
{

}

